package com.etc.traval.daoimpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.etc.traval.dao.OrderDao;
import com.etc.traval.dao.RoomDao;
import com.etc.traval.dao.SellerDao;
import com.etc.traval.dao.UserDao;
import com.etc.traval.util.JDBCUtil;
import com.etc.travel.entity.Order;
import com.etc.travel.entity.Seller;
import com.etc.travel.entity.User;

import oracle.jdbc.OracleTypes;

public class OrderDaoImpl implements OrderDao{
	JDBCUtil util=new JDBCUtil();
	UserDao udao=new UserDaoImpl();
	SellerDao sdao=new SellerDaoImpl();
	RoomDao  rdao=new RoomDaoImpl();
	/**
	 * 通过id查找订单
	 * @author lwh
	 * @return 查到的订单
	 * @param 订单号
	 */
	@Override
	public Order queryById(int id) {
		// TODO Auto-generated method stub
		ResultSet rs=util.query("select * from t_order where order_id=?", id);
		Order order=null;
		try {
			if(rs.next()){
				order=new Order();
				order.setOrder_id(rs.getInt("order_id"));
				order.setOrder_days(rs.getInt("order_days"));
				order.setOrder_endtime(rs.getTimestamp("order_endtime"));
				order.setOrder_money(rs.getDouble("order_money"));
				order.setOrder_room(rdao.queryRoom(rs.getInt("order_room")));
				order.setOrder_time(rs.getTimestamp("order_time"));
				order.setOrder_user(udao.queryUserById(rs.getInt("order_user")));
				order.setOrder_state(rs.getString("order_state"));
				order.setOrder_seller(sdao.selectSellerById(rs.getInt("order_seller")));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			util.closeAll();
		}
		
		return order;
	}

	/**
	 * 添加订单
	 * @author lwh
	 * @return 添加是否成功
	 * @param 订单
	 */
	@Override
	public boolean addOrder(Order order) {
		// TODO Auto-generated method stub
		return util.update("insert into t_order(order_days,order_endtime,order_money,order_room,order_time,order_user,order_seller,order_state)values(?,?,?,?,?,?,?,?)", order.getOrder_days(),order.getOrder_endtime(),order.getOrder_money(),order.getOrder_room().getRoom_id(),order.getOrder_time(),order.getOrder_user().getUser_id(),order.getOrder_seller().getSeller_id(),order.getOrder_state())>0;
	}

	/**
	 * 删除订单
	 * @author lwh
	 * @return 删除是否成功
	 * @param 订单号
	 */
	@Override
	public boolean delOrder(int id) {
		// TODO Auto-generated method stub
		return util.update("delete from t_order where order_id=?", id)>0;
	}
	/**
	 * 通过用户查找订单
	 * @author lwh
	 * @return 查到的订单
	 * @param 用户
	 */
	@Override
	public List<Order> queryByUser(User user) {
		// TODO Auto-generated method stub
		ResultSet rs=util.query("select * from t_order where order_user=?", user.getUser_id());
		List<Order> list=new ArrayList<Order>();
		try {
			while(rs.next()){
				Order order=new Order();
				order.setOrder_id(rs.getInt("order_id"));
				order.setOrder_days(rs.getInt("order_days"));
				order.setOrder_endtime(rs.getTimestamp("order_endtime"));
				order.setOrder_money(rs.getDouble("order_money"));
				order.setOrder_room(rdao.queryRoom(rs.getInt("order_room")));
				order.setOrder_time(rs.getTimestamp("order_time"));
				order.setOrder_user(udao.queryUserById(rs.getInt("order_user")));
				order.setOrder_state(rs.getString("order_state"));
				order.setOrder_seller(sdao.selectSellerById(rs.getInt("order_seller")));
				list.add(order);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			util.closeAll();
		}
		return list;
	}
	/**
	 * 修改订单
	 * @author lwh
	 * @return 修改是否成功
	 * @param 订单
	 */
	@Override
	public boolean changeOrder(Order order) {
		// TODO Auto-generated method stub
		return util.update("update t_order set order_days=?,order_endtime=?,order_money=?,order_room=?,order_time=?,order_user=?,order_seller=?,order_state=? where order_id=?", order.getOrder_days(),order.getOrder_endtime(),order.getOrder_money(),order.getOrder_room().getRoom_id(),order.getOrder_time(),order.getOrder_user().getUser_id(),order.getOrder_seller().getSeller_id(),order.getOrder_state(),order.getOrder_id())>0;
	}
	/**
	 * 通过用户和状态查找订单
	 * @author lwh
	 * @return 查到的订单
	 * @param 用户，状态
	 */
	@Override
	public List<Order> queryUserSta(User user, String Sta) {
		// TODO Auto-generated method stub
		ResultSet rs=util.query("select * from t_order where order_user=? and order_state=?", user.getUser_id(),Sta);
		List<Order> list=new ArrayList<Order>();
		try {
			while(rs.next()){
				Order order=new Order();
				order.setOrder_id(rs.getInt("order_id"));
				order.setOrder_days(rs.getInt("order_days"));
				order.setOrder_endtime(rs.getTimestamp("order_endtime"));
				order.setOrder_money(rs.getDouble("order_money"));
				order.setOrder_room(rdao.queryRoom(rs.getInt("order_room")));
				order.setOrder_time(rs.getTimestamp("order_time"));
				order.setOrder_user(udao.queryUserById(rs.getInt("order_user")));
				order.setOrder_state(rs.getString("order_state"));
				order.setOrder_seller(sdao.selectSellerById(rs.getInt("order_seller")));
				list.add(order);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			util.closeAll();
		}
		return list;
	}
	/**
	 * 通过用户查找订单并分页
	 * @author lwh
	 * @param user:用户
	 * @param size：页面大小
	 * @param page：所在页面
	 * @return 查到的订单
	 */
	@Override
	public List<Order> queryOrderByPage(User user, int size, int page) {
		// TODO Auto-generated method stub
		List<Integer> list=new ArrayList<Integer>();
		list.add(OracleTypes.CURSOR);
		Object[] obj=util.callHasReturn("{call proc_queryOrderPage}", list, page,size,user.getUser_id());
		ResultSet rs=(ResultSet) obj[0];
		List<Order> list1=new ArrayList<Order>();
		try {
			while(rs.next()){
				Order order=new Order();
				order.setOrder_id(rs.getInt("order_id"));
				order.setOrder_days(rs.getInt("order_days"));
				order.setOrder_endtime(rs.getTimestamp("order_endtime"));
				order.setOrder_money(rs.getDouble("order_money"));
				order.setOrder_room(rdao.queryRoom(rs.getInt("order_room")));
				order.setOrder_time(rs.getTimestamp("order_time"));
				order.setOrder_user(udao.queryUserById(rs.getInt("order_user")));
				order.setOrder_state(rs.getString("order_state"));
				order.setOrder_seller(sdao.selectSellerById(rs.getInt("order_seller")));
				list1.add(order);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				rs.close();	
				util.closeAll();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return list1;
	}
	/**
	 * 通过用户和状态查找订单并分页
	 * @author lwh
	 * @return 查到的订单
	 * @param user:用户
	 * @param sta:状态
	 * @param size：页面大小
	 * @param page：所在页面
	 */
	@Override
	public List<Order> queryUserStaPage(User user, String Sta, int size, int page) {
		List<Integer> list=new ArrayList<Integer>();
		list.add(OracleTypes.CURSOR);
		Object[] obj=util.callHasReturn("{call proc_queryOrderByStaPage}", list, page,size,user.getUser_id(),Sta);
		ResultSet rs=(ResultSet) obj[0];
		List<Order> list1=new ArrayList<Order>();
		try {
			while(rs.next()){
				Order order=new Order();
				order.setOrder_id(rs.getInt("order_id"));
				order.setOrder_days(rs.getInt("order_days"));
				order.setOrder_endtime(rs.getTimestamp("order_endtime"));
				order.setOrder_money(rs.getDouble("order_money"));
				order.setOrder_room(rdao.queryRoom(rs.getInt("order_room")));
				order.setOrder_time(rs.getTimestamp("order_time"));
				order.setOrder_user(udao.queryUserById(rs.getInt("order_user")));
				order.setOrder_state(rs.getString("order_state"));
				order.setOrder_seller(sdao.selectSellerById(rs.getInt("order_seller")));
				list1.add(order);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				rs.close();	
				util.closeAll();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return list1;
	}

	@Override
	public List<Order> queryOrderBySeller(Seller seller) {
		ResultSet rs=util.query("select * from t_order where order_seller=?", seller.getSeller_id());
		List<Order> list=new ArrayList<Order>();
		try {
			while(rs.next()){
				Order order=new Order();
				order.setOrder_id(rs.getInt("order_id"));
				order.setOrder_days(rs.getInt("order_days"));
				order.setOrder_endtime(rs.getTimestamp("order_endtime"));
				order.setOrder_money(rs.getDouble("order_money"));
				order.setOrder_room(rdao.queryRoom(rs.getInt("order_room")));
				order.setOrder_time(rs.getTimestamp("order_time"));
				order.setOrder_user(udao.queryUserById(rs.getInt("order_user")));
				order.setOrder_state(rs.getString("order_state"));
				order.setOrder_seller(sdao.selectSellerById(rs.getInt("order_seller")));
				list.add(order);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			util.closeAll();
		}
		return list;
	}

	@Override
	public boolean updateStateById(String state,int id) {
		
		return util.update("update t_order set order_state =? where order_id=?",state,id)>0;
	}
}
